<template>
  <view>
    <view class="content" v-if="!isTghk">

      <view class="cu-form-group between">
        <view class="title">客户名称</view>
        <input v-model="form.customerName" placeholder="请输入名称"></input>
      </view>
      <view class="cu-form-group">
        <view class="title">业务类型</view>
        <picker @change="returnPickerType" :value="form.customerType" range-key="name" :range="types">
          <view class="picker">
            {{types[form.customerType]?types[form.customerType].name:'请选择类型'}}
          </view>
        </picker>
      </view>

      <view class="cu-form-group">
        <view class="title">获客途径</view>
        <picker @change="returnPickerHktj" range-key="name" :range="hktj">
          <view class="picker">
            {{hktj[form.comeFrom]?hktj[form.comeFrom].name:'请选择获客途径'}}
          </view>
        </picker>
      </view>



      <view class="cu-form-group between" v-if="form.customerType-0">
        <view class="title">企业名称</view>
        <input v-model="form.companyName" placeholder="请输入企业名称"></input>
      </view>


      <view class="cu-form-group">
        <view class="title">业务名称</view>
        <picker @change="returnPickerMethod" :value="form.handingMethod" range-key="name" :range="methods">
          <view class="picker">
            {{methods[form.handingMethod]?methods[form.handingMethod].name:'请选择业务名称'}}
          </view>
        </picker>
      </view>
      <view class="cu-form-group">
        <view class="title">是否有担保人</view>
        <picker @change="returnPickerDbr" :value="hasDbr_v" range-key="name" :range="hasDbr">
          <view class="picker">
            {{hasDbr[hasDbr_v].name}}
          </view>
        </picker>
      </view>
      <view v-if="hasDbr_v-0" class="dbr">
        <view class="cu-form-group between">
          <view class="title">担保人</view>
          <input v-model="form.guarantorName" placeholder="请输入担保人"></input>
        </view>
        <view class="cu-form-group between">
          <view class="title">担保人手机号</view>
          <input v-model="form.guarantorPhone" placeholder="请输入担保人手机号" type="number" maxlength="11"></input>
        </view>
    <!--    <view class="cu-form-group between">
          <view class="title">担保人身份证号</view>
          <input v-model="form.guarantorCardId" placeholder="请输入担保人身份证号" type="idcard" maxlength="18"></input>
        </view>
        <view class="cu-form-group between" style="border-bottom: 1px solid #eee;">
          <view class="title">担保人地址</view>
          <input v-model="form.guarantorAddress" placeholder="请输入担保人地址"></input>
        </view> -->
      </view>


      <!-- <view class="cu-form-group between">
        <view class="title">身份证号</view>
        <input v-model="form.cardId" placeholder="请输入身份证号" type="idcard" maxlength="18"></input>
      </view> -->
      <view class="cu-form-group between">
        <view class="title">需求金额</view>
        <input v-model="form.estimatedAmount" placeholder="请输入需求金额" type="number"></input>
      </view>
      <view class="cu-form-group between" v-if="form.handingMethod-0>0">
        <view class="title">银行名称</view>
        <picker @change="returnPickerBankList" range-key="name" :range="banklist">
          <view class="picker">
            {{banklist[form.bankName]?banklist[form.bankName].name:'请选择银行名称'}}
          </view>
        </picker>
        <!-- <input v-model="form.bankName" placeholder="请输入银行名称"></input> -->
      </view>
      <view class="cu-form-group between">
        <view class="title">手机号</view>
        <input v-model="form.mobile" placeholder="请输入手机号" maxlength="13"></input>
      </view>


      <view class="btn-box">
        <view class="submit-btn" @click="submit">保存</view>
      </view>
    </view>
    <view class="content" v-else>
      <view class="cu-form-group between">
        <view class="title">姓</view>
        <input v-model="form2.name" placeholder="请输入您的姓氏"></input>
      </view>
      <view class="cu-form-group">
        <view class="title">性别 </view>
        <picker @change="returnPickersex" range-key="name" :range="sexs">
          <view class="picker">
            {{sexs[form2.sex]?sexs[form2.sex].name:'请选择您的性别'}}
          </view>
        </picker>
      </view>

      <view class="cu-form-group between">
        <view class="title">手机号</view>
        <input v-model="form2.tel" placeholder="请输入手机号" type="number" maxlength="11"></input>
      </view>

      <view class="cu-form-group">
        <view class="title">兴趣板块</view>
        <picker @change="returnPickerbankuai" range-key="name" :range="bankuai">
          <view class="picker">
            {{bankuai[form2.bankuai]?bankuai[form2.bankuai].name:'请选择您感兴趣的板块'}}
          </view>
        </picker>
      </view>

      <view class="cu-form-group between mb40" v-if="form2.bankuai==0">
        <view class="title">融资金额</view>
        <input v-model="form2.money" placeholder="请输入融资金额" type="idcard" maxlength="18"></input>
      </view>
      <view class="btn-box">
        <view class="submit-btn" @click="submit2">保存</view>
      </view>
    </view>
  </view>
</template>

<script>
import api from '@/api/api.js';
export default {
  data() {
    return {
      isTghk: 0,
      types: [
        { id: 1, name: '个人' },
        { id: 2, name: '企业' },
      ],
      hasDbr: [
        { id: 1, name: '无' },
        { id: 2, name: '有' },
      ],
      methods: [
        { id: 1, name: '垫资' },
        { id: 2, name: '信贷' },
        { id: 3, name: '房产抵押' },
        { id: 4, name: '个人经营贷' },
      ],
      hasDbr_v: 0,
      form: {
        customerName: '',
        customerType: '',
        handingMethod: '',
        cardId: '',
        estimatedAmount: '',
        bankName: '',
        mobile: '',
        guarantorAddress: '',
        guarantorCardId: '',
        guarantorPhone: '',
        guarantorName: "",
        companyName: '',
        comeFrom: '', //获客途径下标
		comeFromId:'',//获客途径值
      },

      form2: {
        name: '', //姓氏
        sex: '', //性别
        tel: '', //电话
        bankuai: '', //板块
        money: '', //融资金额
      },
      sexs: [
        { id: 1, name: '男' },
        { id: 2, name: '女' },
      ],
      bankuai: [
        { id: 4, name: '融资服务' },
        { id: 1, name: '财税管理' },
        { id: 2, name: '会记审计' },
        { id: 3, name: '企业资质' },
        { id: 5, name: '法律咨询' },
      ],
      hktj: [{
		  "id":"","name":""
	  }
        // { id: 1, name: '老客户介绍' },
        // { id: 2, name: '自主开发' },
        // { id: 3, name: '抖音获客' },
        // { id: 4, name: '朋友介绍' },
        // { id: 5, name: '续贷' },
        // { id: 6, name: '商会' },
      ],
      banklist: [{
		  "id":"","name":""
	  }]
        // { id: 1, name: '中国银行' },
        // { id: 2, name: '农业银行' },
        // { id: 3, name: '建设银行' },
        // { id: 4, name: '工商银行' },
        // { id: 5, name: '郑州银行' },
        // { id: 6, name: '民生银行' },
        // { id: 7, name: '农商银行' },
        // { id: 8, name: '中原银行' },
        // { id: 9, name: '邮政银行' },
        // { id: 10, name: '临颍中银富登银行' },
        // { id: 11, name: '舞阳玉川银行' },
        // { id: 12, name: '郾城中原村镇银行' },
        // { id: 13, name: '广发银行' },
        // { id: 14, name: '浦发银行' },
        // { id: 15, name: '招商银行' },
        // { id: 16, name: '中信银行' },
        // { id: 17, name: '平安银行' },
      ,
    }
  },
  onLoad(o) {
    this.banklist = JSON.parse(uni.getStorageSync('dicts')).bankName;
	this.hktj = JSON.parse(uni.getStorageSync('dicts')).comeFrom;
    if (o.t == 'tg' || o.t == 'hk') {
      this.isTghk = 1
    }
    if (o.createUserId) {
      this.form.createUserId = o.createUserId
    }
    if (o.isEdit) {
      uni.setNavigationBarTitle({
        title: `修改信息`
      })
      let item = uni.getStorageSync('editObj')
      if (item.customerType == 1) {
        if (item.handingMethod == 6) {
          item.handingMethod = 3;
        } else if (item.handingMethod == 7) {
          item.handingMethod = 4;
        }
      }
      console.log(item);
      this.form = {
        id: item.customerId,
        customerName: item.customerName,
        customerType: item.customerType - 1,
        comeFrom: item.comeFrom ? item.comeFrom : '',
        handingMethod: item.handingMethod - 1,

        cardId: item.cardId,
        estimatedAmount: item.estimatedAmount,
        bankName: item.bankName,
        mobile: item.mobile,

        guarantorAddress: item.guarantorAddress,
        guarantorCardId: item.guarantorCardId,
        guarantorPhone: item.guarantorPhone,
        guarantorName: item.guarantorName,
        companyName: item.companyName

      }
      if (this.form.customerType == 1) { //企业
        this.methods = [
          { id: 1, name: '垫资' },
          { id: 2, name: '信贷' },
          { id: 3, name: '商户贷' },
          { id: 4, name: '税票贷' },
          { id: 5, name: '抵押贷' },
        ]
      } else { //个人
        this.methods = [
          { id: 1, name: '垫资' },
          { id: 2, name: '信贷' },
          { id: 3, name: '房产抵押' },
          { id: 4, name: '个人经营贷' },
        ]
      }
      if (item.guarantorName) {
        this.hasDbr_v = 1
      }
    }
  },
  methods: {
    returnPickerType(e) {
      this.form.customerType = e.detail.value
      if (e.detail.value === 0) this.form.companyName = ''
      this.form.handingMethod = ''
      console.log(123, e);
      if (this.form.customerType == 1) { //企业
        this.methods = [
          { id: 1, name: '垫资' },
          { id: 2, name: '信贷' },
          { id: 3, name: '商户贷' },
          { id: 4, name: '税票贷' },
          { id: 5, name: '抵押贷' },
        ]
      } else { //个人
        this.methods = [
          { id: 1, name: '垫资' },
          { id: 2, name: '信贷' },
          { id: 6, name: '房产抵押' },
          { id: 7, name: '个人经营贷' },
        ]
      }
    },
    returnPickerDbr(e) {
      console.log(e);
      this.hasDbr_v = e.detail.value
      if (e.detail.value === 0) {
        this.form.guarantorAddress = ''
        this.form.guarantorCardId = ''
        this.form.guarantorPhone = ''
        this.form.guarantorName = ""
      }
    },
    returnPickerMethod(e) {
      this.form.handingMethod = e.detail.value
    },
    returnPickersex(e) {
      console.log(e);
      this.form2.sex = e.detail.value
    },
    returnPickerBankList(e) {
      this.form.bankName = e.detail.value
    },
    returnPickerHktj(e) {
      this.form.comeFrom = e.detail.value;
	  this.form.comeFromId = this.hktj[e.detail.value].id;
    },
    returnPickerbankuai(e) {
      this.form2.bankuai = e.detail.value
    },
    validatePhoneNumber(phoneNumber) {
      let reg = /^(10[0-9]|11[0-9]|12[0-9]|13[0-9]|14[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9])\d{8}$/;

      if (reg.test(phoneNumber)) {
        return true;
      } else {
        return false;
      }
    },
    submit2() {
      if (this.form2.name == '') return this.toast(`请输入您的姓氏`)
      if (this.form2.sex === '') return this.toast(`请选择您的性别`)
      if (this.form2.tel == '') return this.toast(`请输入您的手机号`)
      if (this.form2.bankuai === '') return this.toast(`请选择您感兴趣的板块`)
      console.log(this.form2.bankuai);
      if (this.form2.bankuai == 0 && !this.form2.money) return this.toast(`请输入您的融资金额`)
      let i = this.form2
      api.addCustomerByCus({
        name: i.name,
        sex: i.sex,
        model: i.bankuai,
        acount: i.bankuai == 0 ? i.money : '',
        createId: this.form.createUserId,
        phone: i.tel,
      }).then(res => {
        if (res.code == 200) {
          this.toast('提交成功')
        } else {
          this.toast(res.msg)
        }
      })
    },
	isNumeric(value) {
	  return /^\d+$/.test(value);
	},
    submit() {
      console.log(uni.getStorageSync('dicts').bankName);
      if (!this.form.customerName) {
        uni.showToast({
          title: "请输入名称",
          icon: 'none',
        });
        return
      }
      if (this.form.customerType === '') {
        uni.showToast({
          title: "请选择类型",
          icon: 'none',
        });
        return
      }
      if (this.form.comeFrom === '') {
        uni.showToast({
          title: "请选择获客途径",
          icon: 'none',
        });
        return
      }
      if (this.form.customerType - 0 && !this.form.companyName) {
        uni.showToast({
          title: "请输入公司名称",
          icon: 'none',
        });
        return
      }

      if (this.form.handingMethod === '') {
        uni.showToast({
          title: "请选择办理方式",
          icon: 'none',
        });
        return
      }
      if (this.hasDbr_v - 0) {
        if (!this.form.guarantorName) {
          uni.showToast({
            title: "请输入担保人姓名",
            icon: 'none',
          });
          return
        }
        if (!this.validatePhoneNumber(this.form.guarantorPhone)) {
          uni.showToast({
            title: "请输入正确的担保人手机号",
            icon: 'none',
          });
          return
        }
        if (!this.form.guarantorCardId) {
          uni.showToast({
            title: "请输入担保人身份证",
            icon: 'none',
          });
          return
        }
        if (!this.form.guarantorAddress) {
          uni.showToast({
            title: "请输入担保人地址",
            icon: 'none',
          });
          return
        }
      }
      // if (!this.form.cardId) {
      //   uni.showToast({
      //     title: "请输入身份证号",
      //     icon: 'none',
      //   });
      //   return
      // }
      if (this.form.estimatedAmount - 0 <= 0) {
        uni.showToast({
          title: "请输入正确的需求金额",
          icon: 'none',
        });
        return
      }
	  
	  if (!this.isNumeric(this.form.estimatedAmount)){
		  uni.showToast({
		    title: "需求金额必须是纯数字",
		    icon: 'none',
		  });
		  return
	  }
	  
      if (!this.validatePhoneNumber(this.form.mobile.replace(/\s+/g, ''))) {
        uni.showToast({
          title: "请输入正确的手机号",
          icon: 'none',
        });
        return
      }
	  if ((this.form.handingMethod - 0 !== 0)){
		  if (this.form.bankName === '') {
			uni.showToast({
			  title: "请输入银行名称",
			  icon: 'none',
			});
			return
		  }else{
			console.log(this.form.bankName);
			this.form.bankName = this.banklist[this.form.bankName].name;
			console.log(this.form.bankName);
		  }
	  }else{
		  this.form.bankName = null;
	  }
      let data = JSON.parse(JSON.stringify(this.form))
      console.log(data);
      data.customerType = data.customerType - 0 + 1
      data.handingMethod = data.handingMethod - 0 + 1
      console.log(data.handingMethod);
      if (data.customerType == 1) {
        if (data.handingMethod == 3) {
          data.handingMethod = 6;
        } else if (data.handingMethod == 4) {
          data.handingMethod = 7;
        }
      }
      data.comeFrom = data.comeFromId;
	  data.mobile = data.mobile.replace(/\s+/g, '');
      if (this.form.createUserId) { //自己提交
        api.addCustomerByCus(data).then(res => {
          if (res.code == 200) {
            this.toast('提交成功')
          } else {
            this.toast(res.msg)
          }
        })
        return
      }

      if (this.form.id) { //编辑
		data.mobile = data.mobile.replace(/\s+/g, '');
        data.customerId = data.id
        api.editCustomer(data).then(
            res => {
              if (res.code == 200) {
                uni.showToast({
                  title: '操作成功',
                  duration: 2000
                });

                setTimeout(function() {
                  uni.navigateBack({
                    success: () => {}
                  })
                }, 500);
              } else {
                uni.showToast({
                  title: res.msg,
                  icon: 'none',
                });
              }

            },
            rej => {}
        );
        return
      }

      api.addCustomer(data).then(
          res => {

            if (res.code == 200) {
              console.log(978, this.role);
              if (this.role != '业务员' && this.role != '超级管理员' && this.role != '业务主管' && this.role != '董事长') {
                this.toast('操作成功')
                setTimeout(function() {
                  uni.navigateTo({
                    url: `/pages/customer/index2`
                  })
                }, 500);
                return
              }


              //提交审核
              api.approval({
                customerId: res.data.customerId,
                approvalStatus: 0,
              }).then(res => {
                if (res.code == 200) {
                  this.toast('操作成功')
                  setTimeout(() => {
                    if (this.role == '业务员') {
                      uni.switchTab({
                        url: `/pages/customer/index`
                      })
                    } else {
                      uni.navigateTo({
                        url: `/pages/customer/index2`
                      })
                    }
                  }, 500);
                } else {
                  this.toast(res.msg)
                }
              })
            } else {
              this.toast(res.msg)
            }
          },
          rej => {}
      );

    }
    // 来源
    // sourcePicker(e) {
    // 	this.index = e.detail.value
    // },
  }
}
</script>

<style scoped lang="scss">
.content {
  margin-top: 20upx;

  .floow-cont {
    flex-direction: column;
    background-color: #ffffff;
    padding: 1rpx 30rpx;
    margin-top: 20upx;

    .title {
      text-align: justify;
      padding-right: 30upx;
      font-size: 30upx;
      position: relative;
      height: 60upx;
      line-height: 60upx;
    }

    textarea {
      width: 100%;
      height: 200upx;
      padding: 20upx 0;
    }
  }
}

.btn-box {
  padding: 20upx 24upx;

  .submit-btn {
    height: 80upx;
    line-height: 80upx;
    text-align: center;
    font-size: 28upx;
    color: #fff;
    border-radius: 8upx;
    background: #39b54a;
  }
}

.dbr {
  .title {
    text-indent: 2em;
  }
}
</style>